var FRIENDSHIPS;
$(document).ready(function(){
FRIENDSHIPS={
	close_insert_friend:function(fid,vt,fstr){//truyen vao id ban, vi tri, danh sach id ban da hien thi lay tu the input 'str_id_friend'
		$('.fclose_'+fid).fadeOut(350);
		$.ajax({
			url:ROOT+'user/index/get1friendrand',
			type:'post',
			data:{str_id_friend:fstr, vitri:vt},
			success:function(data){
				//alert(data);
				var obj = jQuery.parseJSON(data);
				var id = obj.id; var user_firstname = obj.user_firstname; var user_lastname = obj.user_lastname; var banchung = obj.banchung; var user_avata = obj.user_avata; var str_id_friend = obj.str_id_friend;
				$('.friend_vt_'+vt).html("<div class='friend fclose_"+id+"'><div class='friend_close' title='Close friend' onclick='FRIENDSHIPS.close_insert_friend("+id+", "+vt+", document.getElementById(\"str_id_friend\").value);'></div><img src='/hoidap/Source/public/images/ava.jpg' alt=''><h1>"+user_lastname+" "+user_firstname+"</h1><h2>"+banchung+" bạn chung</h2><h3><img src='/hoidap/Source/public/images/addF.jpg' alt=''><a class='btn_themban' onclick='addfriend("+id+",1,"+vt+", document.getElementById(\"str_id_friend\").value)' href='javascript:;'>Thêm bạn</a></h3></div>");
				document.getElementById('str_id_friend').value = str_id_friend;
			}
		});
	},
}
});

function addfriend(u1,u2,vt,str_id_friend){//truyen vao u1:user gui request; u2:iduser minh; vt:vitri; Chuoi id friend da hien thi
 	$.ajax({
			url:ROOT+'friendship/index/addfriend',
			type:'post',
			dataType:'html',
			data:{user1:u1, user2:u2},
			success:function(data){
				var obj = jQuery.parseJSON(data);
				var firstname = obj.firstname; var lastname = obj.lastname; var avata = obj.avata;
				$('.div_ctn_tb').append("<li class='cursor_d bd_bottom request_ketban_"+u1+"'><div class='img_user_tb'><img src='/hoidap/source/public/images/ava.jpg' alt='' title='user'/></div><div class='nd_tb'><div class='title_tb'>"+lastname+" "+firstname+"</div><div class='date_tb'>Khoảng 3 ngày trước</div><div class='xacnhan_kb_1 xn_kb_"+u1+"'><a onclick='xacnhanketban("+u1+", "+u2+");' href='javascript:;'>Xác nhận</a></div><div class='xacnhan_kb_2 kpln_kb_"+u1+"'><a onclick='khongphailucnay("+u1+", "+u2+");' href='javascript:;'>Không phải lúc này</a></div></div></li>");
				FRIENDSHIPS.close_insert_friend(u1,vt,str_id_friend);
			}
		});
	getnumrequestfriendships(u2);
}

function xacnhanketban(iu1, iu2){//iduser gui request, iduser minh
	$.ajax({
		url:ROOT+'friendship/index/acceptrequestfriends',
		type:'post',
		data:{iduser1:iu1, iduser2:iu2},
		success:function(data){
				//alert(data);
				$('.xn_kb_'+iu1).fadeOut();
				$('.kpln_kb_'+iu1).fadeOut();
				return false;
			}
	});
	getnumrequestfriendships(iu2);
}

function khongphailucnay(iu1, iu2){//iduser gui request, iduser minh
	$.ajax({
		url:ROOT+'friendship/index/unacceptrequestfriends',
		type:'post',
		data:{iduser1:iu1, iduser2:iu2},
		success:function(data){
				//alert(data);
				//$('.xn_kb_'+iu1).fadeOut();
				//$('.kpln_kb_'+iu1).fadeOut();
				$('li.request_ketban_'+iu1).fadeOut();
				return false;
			}
	});
	getnumrequestfriendships(iu2);
}

function getnumrequestfriendships($iduser){
	var iu = $iduser;
	$.ajax({
		url:ROOT+'friendship/index/getnumrequestfriendships',
		type:'post',
		data:{iduser:iu},
		success:function(data){
			var obj = jQuery.parseJSON(data);
			var num_friend_request = obj.num_friend_request;
			//alert(num_friend_request);
			
			if(num_friend_request!=0){
				$('.rq_tb_kb').css("display","inline");
				$('.num_request_kb').html(num_friend_request);
			}else if(num_friend_request==0){
				$('.rq_tb_kb').css("display","none");
			}
		}
	});
}

//Lay thong tin Request khi nhap bao Btnrequest
function getinfouserrequestfriendships(iu){
	$.ajax({
			url:ROOT+'friendship/index/getrequestfriendships',
			type:'post',
			data:{iduser:iu},
			success:function(data){
				$('.div_ctn_tb').html(data);
				if(data=="")
					$('.div_ctn_tb').html("Không có yêu cầu mới");
			}
		});
}

$(document).ready(function(){
	var iduser = document.getElementById('input_iduser').value;
	//Moi load trang se tim kiem ban be request
	getnumrequestfriendships(iduser);
	//getinfouserrequestfriendships(iduser);
	//Khi click vao chap nhan hoac khong phai luc lai thi div nd_ketban khong bi mat
	$('.nd_ketban').click(function(){
		return false;
	});
});


$(document).ready(function(){
	$('.nd_ketban').hide();
	$('.nd_tinnhan').hide();
	$('.nd_thongbao').hide();
	$('li.tb_ketban').live('click',function(){
		$('.div_ctn_tb').html("<img src='"+ROOT+"public/images/ajax-loading.gif'/>");
		setTimeout('getinfouserrequestfriendships(1)', 800);
		$('.nd_ketban').show(); $('.nd_tinnhan').hide(); $('.nd_thongbao').hide();
		$('.tb_ketban').addClass('tb_ketban_on');
		$('.tb_tinnhan_on').removeClass('tb_tinnhan_on');
		$('.tb_thongbao_on').removeClass('tb_thongbao_on');
	});
	$('li.tb_ketban_on').live('click',function(){
		$('.nd_ketban').hide(); $('.tb_ketban_on').removeClass('tb_ketban_on');
	});
	
	$('.tb_tinnhan').live('click',function(){
		$('.nd_ketban').hide(); $('.nd_tinnhan').show(); $('.nd_thongbao').hide();
		$('.tb_tinnhan').addClass('tb_tinnhan_on');
		$('.tb_ketban_on').removeClass('tb_ketban_on');
		$('.tb_thongbao_on').removeClass('tb_thongbao_on');
	});
	$('.tb_tinnhan_on').live('click',function(){
		$('.nd_tinnhan').hide(); $('.tb_tinnhan_on').removeClass('tb_tinnhan_on');
	});
	
	$('.tb_thongbao').live('click',function(){
		$('.nd_ketban').hide(); $('.nd_tinnhan').hide(); $('.nd_thongbao').show();
		$('.tb_thongbao').addClass('tb_thongbao_on');
		$('.tb_ketban_on').removeClass('tb_ketban_on');
		$('.tb_tinnhan_on').removeClass('tb_tinnhan_on');
	});
	$('.tb_thongbao_on').live('click',function(){
		$('.nd_thongbao').hide(); $('.tb_thongbao_on').removeClass('tb_thongbao_on');
	});
	$('body').click(function(){
		$('.nd_ketban').hide();
		$('.nd_tinnhan').hide();
		$('.nd_thongbao').hide();
	});
	$('.menudown').live('click',function(){
		$('.boxmenudown').show();
		$('.menudown').addClass('menudown_on');
		$('.menudown_on').removeClass('menudown');
	});
	$('.menudown_on').live('click',function(){
		$('.boxmenudown').hide();
		$('.menudown_on').addClass('menudown');
		$('.menudown').removeClass('menudown_on');
	});

	$('*').keyup(function(e){
        if(e.keyCode=='27'){
			$('.nd_ketban').hide();
			$('.nd_tinnhan').hide();
			$('.nd_thongbao').hide();
        }         
    });
});



//Lightbox hinh anh
	jQuery(function($) {
	    var $chosenSheet,
		$stylesheets = $( "a[id^=theme-]" );
	    
	    // theme switcher
	    $stylesheets.click(
		function() {
		    $chosenSheet = $( this ).attr( "href" );
		    $( "#theme").attr( "href", $chosenSheet );
		    
		    // mark this A as an active
		    $stylesheets.removeClass( "active" );
		    $( this ).addClass( "active" );
		    return false;
		}
	    );
	    
	    // run rlightbox
	    $( ".lb" ).rlightbox();
	    $( ".lb_title-overwritten" ).rlightbox({overwriteTitle: true});
	});




















